<?php

/**
 * @author nhatnam
 * @copyright 2011
 */
//tao function ket noi

include('clsketnoi.php');

function taomaquydinh()
{
    return taomatudong("select maqd from tblquydinh","maqd", 3, 15);
}
//them du lieu
function themquydinh()
{
    $tuoitt = $_POST['tuoitoithieu'];
    $maqd= mysql_real_escape_string($_POST["maqd"]);
    $tuoitd =$_POST["tuoitoida"];
    $sosachmtd =$_POST["sosachmuontoida"];
    $thoigianmtd =$_POST["thoigianmuontoida"];
    $thoihansd = $_POST['thoihansudung'];
    $sachxbtv = $_POST['sachxuatbantrongvong'];
    $tremotngay = $_POST['tremotngayphat'];
    
    mysql_query("insert into tblquydinh (maqd, tuoitoithieu, tuoitoida, thoihansudung, sachxuatbantrongvong, thoigianmuontoida, tremotngayphat, sosachmuontoida)".
                " values('$maqd','$tuoitt','$tuoitd','$thoihansd','$sachxbtv','$thoigianmtd','$tremotngay','$sosachmtd')",dbketnoi());
    mysql_close();
}
//xoa nhan vien
function xoaquydinh()
{
    $maqd= mysql_real_escape_string($_POST["maqd"]);
    mysql_query("delete from tblquydinh where maqd='$maqd'",dbketnoi());
    mysql_close();
}
//update nhan vien
function updatequydinh()
{
    $tuoitt = $_POST['tuoitoithieu'];
    $maqd= mysql_real_escape_string($_POST["maqd"]);
    $tuoitd =$_POST["tuoitoida"];
    $sosachmtd =$_POST["sosachmuontoida"];
    $thoigianmtd =$_POST["thoigianmuontoida"];
    $thoihansd = $_POST['thoihansudung'];
    $sachxbtv = $_POST['sachxuatbantrongvong'];
    $tremotngay = $_POST['tremotngayphat'];
    
    mysql_query("update tblquydinh set  tuoitoithieu='$tuoitt', tuoitoida='$tuoitd', sosachmuontoida='$sosachmtd', thoihansudung='$thoihansd', "."
                                        thoigianmuontoida='$thoigianmtd', sachxuatbantrongvong='$sachxbtv', tremotngayphat='$tremotngay'",dbketnoi());
    mysql_close();
}

// hien thi du lieu
function hienthiquydinh()
{
    $limit = 15; //gioi han so row trong 1 trang

    $page = isset ( $_GET["page"] ) ? intval ( $_GET["page"] ) : 1; //get trang hien thi

    $page_start = ( $page - 1 ) * $limit;
    $page_end = $page * $limit;

    $query = mysql_query("SELECT maqd, tuoitoithieu, tuoitoida, thoihansudung, sachxuatbantrongvong, thoigianmuontoida,"." 
                                    sosachmuontoida, tremotngayphat FROM tblquydinh",dbketnoi());

    $number_of_page = ceil(mysql_num_rows($query)/$limit); // lay tong row

    if ($number_of_page > 1)
    {
        $list_page = " <td> Trang: </td>";

        for ( $i = 1; $i <= $number_of_page; $i++ )
            {
            if ( $i == $page )
            {
                $list_page .= " <td>[ <b>{$i}</b> ]</td> ";
           
            }
            else
            {
                $list_page .= "<td><a href='giaodienquydinh.php?page={$i}'> {$i} </a></td>";
                
            }
        }
    }
    taotable();
    $i = 0;
    while ( $result = mysql_fetch_array ( $query ) )
    {
        if ( $i >= $page_start )
        {
            //mau bg cho dong chan le
            if($i%2==0)
                $bg = "#d0eafb";
            else
                $bg = "#66aee0";
            
            //tao ra 1 table bang lenh ket hop css
            echo "<tr bgcolor='".$bg."'>"; 
            echo "<td><a href='' >".$result['maqd']."</a></td>";
            echo "<td>".$result['tuoitoithieu']."</td>";
            echo "<td>".$result['tuoitoida']."</td>";
            echo "<td>".$result['thoihansudung']."</td>";
            echo "<td>".$result['sachxuatbantrongvong']."</td>";
            echo "<td>".$result['thoigianmuontoida']."</td>";
            echo "<td>".$result['sosachmuontoida']."</td>";
            echo "<td>".$result['tremotngayphat']."</td>";
            echo "</tr>";
        }

        $i++;

        if ($i >= $page_end)
        {
            break;
        }
    }
    echo "</table></br>";
    echo $list_page;
}
function taotable()
{
    $title = array("Mã QĐ ","Tuổi TT","Tuổi TĐ","Thời Hạn SD", "Sách XBTV", "Thời Gian MTĐ", "Số Sách MTĐ", "Trễ MNP");
    echo "<table style='border-color:black;'  cellspacing='1' width='670'>";
    echo "<tr>";
    for($i=0; $i<=7; $i++)
    {
        echo "<th style='background-color:#2287cd; color='white';>".$title[$i]."</th>"; 
    }
    echo "</tr>";
  
}

?>
